package com.umfwechat.core.interceptor;

import org.springframework.ui.ModelMap;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.request.WebRequestInterceptor;

import com.umfwechat.core.loggers.LoggersFactory;

/**
 * 性能日志输出拦截器
 * Created by hopeone on 2017/5/11.
 */
public class SSLogInterceptor implements WebRequestInterceptor{


	public void afterCompletion(WebRequest request, Exception ex) throws Exception {
		long startTime = (Long) request.getAttribute("requeststarttime", 0);
		long tookTime = System.currentTimeMillis() - startTime;
		String uri = "";
		String requestStr = request.toString();
		String[] rs1 = requestStr.split(";");
		if(rs1.length == 2){
			String[] uriStr = rs1[0].split("=");
			if(uriStr.length == 2){
				uri = uriStr[1];
			}
		}
		StringBuilder ssSb = new StringBuilder();
		ssSb.append("IP").append(",");
		ssSb.append("Service").append(",");
		ssSb.append(uri).append(",");
		ssSb.append(tookTime).append(",");
		
		LoggersFactory.SS_LOGGER().info(ssSb.toString());
		/**
		 * 结束的地方输出性能日志
		 * 输出服务耗时日志信息
		 * 遵守性能监控日志规范:
		 * 
		 */
		
	}


	public void postHandle(WebRequest request, ModelMap modelMap) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("SSLogInterceptor-postHandle");
	}


	public void preHandle(WebRequest request) throws Exception {
		long startTime = System.currentTimeMillis();
		request.setAttribute("requeststarttime", startTime, 0);
		System.out.println("SSLogInterceptor-preHandle");
		
	}

		
	
	
}
